package csce315.group10.androidapp;

import java.util.ArrayList;
import java.util.List;

import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;

import com.dropbox.client2.DropboxAPI;
import com.dropbox.client2.android.AndroidAuthSession;
import com.dropbox.client2.session.AccessTokenPair;
import com.dropbox.client2.session.AppKeyPair;
import com.dropbox.client2.session.Session.AccessType;

import android.app.Application;
import android.util.Log;

@Root
public class CustomApp extends Application{
	@ElementList(name = "contact_list")
	private ArrayList<Contact> contact_list;
	@ElementList(name = "event_list")
	private ArrayList<Event> event_list;
	@ElementList(name = "pot_story_list")
	private ArrayList<PotentialStory> pot_story_list;
	private Contact admin;
	private Contact current_user;
	/* information to access dropbox server */
	final static private String APP_KEY = "b5yb23zfjkwkiwy";
	final static private String APP_SECRET = "59z5vugx2j18v31";
	final static private AccessType ACCESS_TYPE = AccessType.APP_FOLDER;
	@Element(name = "Dropbox_key",required = false)
	private String db_key = "";
	@Element(name = "Dropbox_secret",required = false)
	private String db_secret = "";
	private DropboxAPI<AndroidAuthSession> mDBApi;
	AndroidAuthSession session;
	AppKeyPair appKeys;
	
	public CustomApp(){
		admin = new Contact("Admin","","","","Editor And Chief");
		admin.setPassword("password", "Reveille");
		contact_list = new ArrayList<Contact>();
		event_list = new ArrayList<Event>();
		pot_story_list = new ArrayList<PotentialStory>();
	}
	/* to build CustomApp from file */
	/*************/
	public void set_contact_list(ArrayList<Contact> _contact_list){
		contact_list = _contact_list;
	}
	public void set_event_list(ArrayList<Event> _event_list){
		event_list = _event_list;
	}
	public void set_pot_story_list(ArrayList<PotentialStory> _pot_story_list){
		pot_story_list = _pot_story_list;
	}
	/*************/
	/* getters */
	public ArrayList<Contact> GetContacts(){
		return contact_list;
	}
	public ArrayList<Event> GetEvents(){
		return event_list;
	}
	public ArrayList<PotentialStory> GetPStories(){
		return pot_story_list;
	}
	public void set_current_user(Contact user){
		current_user = user;
	}
	public Contact get_current_user(){
		return current_user;
	}
	public Contact get_admin(){
		return admin;
	}
	public boolean is_user(String user){
		for(int i = 0; i < contact_list.size(); i++)
		{
			if(contact_list.get(i).get_name().equals(user)){
				return true;
			}
		}
		return false;
	}
	public Contact getUser(String name){
		for(int i = 0; i < contact_list.size(); i++)
		{
			if(contact_list.get(i).get_name().equals(name)){
				return contact_list.get(i);
			}
		}
		return null;
	}
	
	/* Create Dropbox Session */
	public void initializeDropbox(){
		/* if authenticated previously use previous information */
		appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
		session = new AndroidAuthSession(appKeys, ACCESS_TYPE);
		if(db_key != "" && db_secret != ""){
			AccessTokenPair access = new AccessTokenPair(db_key,db_secret);
			session.setAccessTokenPair(access);
			Log.d("DbLog", "Access Token set");
		}
		mDBApi = new DropboxAPI<AndroidAuthSession>(session);
	}
	/* access dropbox session */
	public DropboxAPI<AndroidAuthSession> get_dropbox(){
		return mDBApi;
	}
	/* save keys so user does not have to re-authenticate each use */
	public void store_keys(String key,String secret){
		db_key = key;
		db_secret = secret;
	}
	
	/* get dropbox key */
	public String get_dbkey(){
		return db_key;
	}
	/* get dropbox secret */
	public String get_dbsecret(){
		return db_secret;
	}
	
	public ArrayList<Contact> getContactList(){
		return contact_list;
	}
}
